package com.example.sc.learn.controller;

import com.example.sc.learn.entity.User;
import com.example.sc.learn.service.UserService;
import com.example.sc.learn.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RequestMapping(value = "user")
@RestController
@Api(value = "用户Controller",description = "用户Controller")
public class UserController {



    @Autowired
    private UserService userService;


    @ApiOperation(value = "新增用户信息")
    @PostMapping
    public ResponseData<Boolean> insertUser(@RequestBody User user){
        try {
            userService.insertUser(user);
            return new ResponseData<>(true);
        }catch (Exception e){
            log.error("insert user runtime exception", e);
        }
        log.info("insert user success");

        return new ResponseData<>(false);
    }



    @ApiOperation(value = "获取用户信息", notes = "根据用户编号")
    @ApiImplicitParam(name = "userCode",  value = "用户编号",  paramType = "path", required = true)
    @GetMapping("{userCode}")
    public ResponseData<User> selectByUserCode(@PathVariable(value = "userCode") String userCode){
        User user = null;
        try {
            user = userService.selectByUserCode(userCode);
        }catch (Exception e){
            log.error("find user runtime exception", e);
        }
        log.info("find user result: {}", user);
        return new ResponseData<>(user);
    }



}
